package cn.wsalix.admin.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.RelatedRepository;
import cn.wsalix.admin.entity.Permit;
import cn.wsalix.admin.entity.RolePermit;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.form.RolePermitForm;

/**
 * @author jskyme
 * 
 */
public interface RolePermitRepository extends
		RelatedRepository<RolePermitForm, RolePermit, SysRole, Permit> {

	@Query("select rp from UserRole ur,RolePermit rp where ur.preFix.id = :userId and ur.postFix.id= rp.preFix.id")
	List<RolePermit> findRolePermsByUserId(@Param("userId") Long userId);
	
	@Query("select rp.postFix from UserRole ur,RolePermit rp where ur.preFix.id = :userId and ur.postFix.id= rp.preFix.id")
	List<Permit> findPermsByUserId(@Param("userId") Long userId);
	
	@Query("select o from RolePermit o where o.postFix.id = :permitId")
	List<RolePermit> findsByPermitId(@Param("permitId") Long permitId);

	@Query("select o from RolePermit o where o.preFix.id = :roleId")
	List<RolePermit> findsByRoleId(@Param("roleId") Long roleId);

	@Query("select o from RolePermit o where o.postFix.id <> :permitId")
	List<SysRole> findUnRolesByPermitId(@Param("permitId") Long permitId);
	
	@Query("select o from RolePermit o where o.preFix.id <> :roleId")
	List<Permit> findUnPermitsByRoleId(@Param("roleId") Long roleId);
}
